package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpServiceJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * CTassName: EmpControllerJob
 * Package: com.itheima.controller
 * Description:
 *
 * @Author xw
 * @Create 2023/11/4 21:59
 * @Version 1.0
 */

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpControllerJob {

    @Autowired
    private EmpServiceJob empService;

    //条件分页查询
    @GetMapping
    public Result page(
            String name,
            Short gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize


    ) {
        //记录日志
        log.info("分页查询，参数：name = {},gender = {},begin = {}, end = {}", name, gender, begin, end);
        log.info("分页查询，参数：page = {},pageSize = {}", page, pageSize);
        //调用业务层分页查询功能
        PageBean pageBean = empService.page(name, gender, begin, end, page, pageSize);
        //响应
        return Result.success(pageBean);
    }


    // 添加员工
    @PostMapping
    public Result addEmp(@RequestBody Emp e) {
        //记录日志
        log.info("添加员工，参数：{}", e.toString());
        //调用业务层添加功能
        empService.add(e);
        return Result.success();
    }


    // 删除
    @DeleteMapping("/{ids}")
    public Result deleteEmp(@PathVariable String ids) {

        //记录日志
        log.info("deleteEmp，参数：{}---{}", ids, ids.split(","));


        // 这里你可以将 ids 字符串按照逗号分割，然后处理每一个 id
        String[] idArray = ids.split(",");

        //  调用业务层删除功能
        empService.deleteEmp(idArray);

        return Result.success();
    }

    //根据id查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        //记录日志
        log.info("根据id查询，参数：{}", id);
        //  调用业务层id查询功能
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }


    // 修改
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        //记录日志
        log.info("根据修改，参数：{}", emp);
        //  调用业务层修改员工
        empService.update(emp);

        return Result.success();
    }


}
